package cn.baage.campus.config.captcha;

import com.google.code.kaptcha.text.impl.DefaultTextCreator;

import java.util.Random;

/**
 * @Description: 自定义验证码生成器(数学)
 * @Author: 八阿哥
 * @url: baage.cn
 * @Version: 1.0
 **/
@SuppressWarnings({"unused"})
public class MathTextCreator extends DefaultTextCreator
{
    @Override
    public String getText()
    {
        int result;
        Random random = new Random();
        // 生成 1 ~ 10 的随机值
        int x = random.nextInt(10) + 1;
        int y = random.nextInt(10) + 1;
        StringBuilder suChinese = new StringBuilder();
        // 生成随机计算符
        int randomOperands = random.nextInt(3);
        int largerOperand = Math.max(x, y);
        int smallerOperand = Math.min(x, y);
        String operator = "*";
        result = largerOperand * smallerOperand;
        suChinese.append(largerOperand);
        if (randomOperands == 1) {
            operator = "-";
            result = largerOperand - smallerOperand;
        } else if (randomOperands == 2){
            operator = "+";
            result = largerOperand + smallerOperand;
        }
        suChinese.append(operator).append(smallerOperand).append("=?@").append(result);
        return suChinese.toString();
    }
}